/*
 *   Copyright (c) 2014, Andrew Romanenko <melanhit@gmail.com>
 *   All rights reserved.
 *
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright notice, this
 *      list of conditions and the following disclaimer.
 *   2. Redistributions in binary form must reproduce the above copyright notice,
 *      this list of conditions and the following disclaimer in the documentation
 *      and/or other materials provided with the distribution.
 *   3. Neither the name of the project nor the names of its contributors
 *      may be used to endorse or promote products derived from this software
 *      without specific prior written permission.
 *
 *   THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
 *   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 *   ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdlib.h>
#include <stdint.h>
#include <string.h>

#include "../akmos.h"
#include "../cipher.h"

#include "cbc.h"

int akmos_cbc_init(akmos_cipher_t *ctx)
{
    akmos_cbc_t *ptr;

    ptr = (akmos_cbc_t *)ctx->mctx;

    ptr->iv = malloc(ctx->algo->blklen * 2);
    if(!ptr->iv)
        return AKMOS_ERR_ENOMEM;

    ptr->buf = ptr->iv + ctx->algo->blklen;

    return AKMOS_ERR_SUCCESS;
}

void akmos_cbc_setiv(akmos_cipher_t *ctx, const uint8_t *iv)
{
    akmos_cbc_t *ptr;

    ptr = (akmos_cbc_t *)ctx->mctx;

    if(!iv)
        memset(ptr->iv, 0, ctx->algo->blklen);
    else
        memcpy(ptr->iv, iv, ctx->algo->blklen);
}

void akmos_cbc_encrypt(akmos_cipher_t *ctx, const uint8_t *in_blk, size_t in_len, uint8_t *out_blk)
{
    akmos_cbc_t *ptr;
    size_t i, j, n, nb, blklen;
    uint32_t *p1, *p2;

    ptr = (akmos_cbc_t *)ctx->mctx;
    blklen = ctx->algo->blklen;

    n = blklen / sizeof(uint32_t);
    nb = in_len / blklen;

    p1 = (uint32_t *)ptr->iv;
    p2 = (uint32_t *)in_blk;

    for(i = 0; i < nb; i++, out_blk += blklen) {
        for(j = 0; j < n; j++, p2++)
            p1[j] ^= *p2;

        ctx->algo->encrypt(ctx->actx, ptr->iv, out_blk);

        memcpy(ptr->iv, out_blk, blklen);
    }
}

void akmos_cbc_decrypt(akmos_cipher_t *ctx, const uint8_t *in_blk, size_t in_len, uint8_t *out_blk)
{
    akmos_cbc_t *ptr;
    size_t i, j, n, nb, blklen;
    uint32_t *p1, *p2;

    ptr = (akmos_cbc_t *)ctx->mctx;
    blklen = ctx->algo->blklen;

    n = blklen / sizeof(uint32_t);
    nb = in_len / blklen;

    p1 = (uint32_t *)ptr->iv;
    p2 = (uint32_t *)ptr->buf;

    for(i = 0; i < nb; i++, in_blk += blklen, out_blk += blklen) {
        ctx->algo->decrypt(ctx->actx, in_blk, ptr->buf);

        for(j = 0; j < n; j++)
            p2[j] ^= p1[j];

        memcpy(ptr->iv, in_blk, blklen);
        memcpy(out_blk, ptr->buf, blklen);
    }
}

void akmos_cbc_free(akmos_cipher_t *ctx)
{
    akmos_cbc_t *ptr;

    if(!ctx)
        return;

    ptr = (akmos_cbc_t *)ctx->mctx;

    if(ptr->iv) {
        memset(ptr->iv, 0, ctx->algo->blklen * 2);
        free(ptr->iv);
    }

    memset(ptr, 0, sizeof(akmos_cbc_t));
    free(ptr);
}
